package com.atmaowei.controller;

import com.atmaowei.common.Result;
import com.atmaowei.controller.request.DepartmentPageRequest;
import com.atmaowei.entity.Department;
import com.atmaowei.entity.Student;
import com.atmaowei.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    @PostMapping("/save")
    public Result save(@RequestBody Department department) {
        departmentService.save(department);
        return Result.success();
    }

    @DeleteMapping("/delete/{dno}")
    public Result delete(@PathVariable String dno) {
        departmentService.delete(dno);
        return Result.success();
    }

    @PutMapping("/update/{oldDname}")
    public Result update(@RequestBody Department department,@PathVariable String oldDname) {
        departmentService.update(department,oldDname);
        return Result.success();
    }

    @GetMapping("/{dno}")
    public Result getByDno(@PathVariable String dno) {
        Department department = departmentService.getByDno(dno);
        return Result.success(department);
    }

    @GetMapping("/page")
    public Result page(DepartmentPageRequest departmentPageRequest) {
        return Result.success(departmentService.page(departmentPageRequest));
    }

    @PostMapping("/deleteSelected")
    public Result deleteSelected(@RequestBody List<Department> departments) {
        departmentService.deleteSelected(departments);
        return Result.success();
    }

}
